from . import web
from flask_login import login_required, current_user
from app.libs.email import send_mail
from app.models.base import db
from app.models.gift import Gift
from app.models.wish import Wish
from flask import current_app, flash,redirect, url_for, render_template
from app.view_models.trade import MyTrades

__author__ = '七月'


@web.route('/my/wish')
def my_wish():
    uid = current_user.id
    wishes_of_mine = Wish.get_user_wishes(uid)
    isbn_list = [wish.isbn for wish in wishes_of_mine]
    gift_count_list = Wish.get_gift_counts(isbn_list)
    view_model=MyTrades(wishes_of_mine,gift_count_list)
    return render_template("my_wish.html",wishes=view_model.trades)


@web.route('/wish/book/<isbn>')
@login_required
def save_to_wish(isbn):
    if current_user.can_save_to_list(isbn):
        with db.auto_commit():
        # try:
            wish=Wish()
            wish.isbn=isbn
            wish.uid=current_user.id
            db.session.add(wish)
            # db.session.commit()
        # except Exception as e:
        #     db.session.rollback()
        #     raise e
    else:
        flash("这本书已添加至您的的赠送清单或已存在您的心愿清单，请不要重复添加")
    return redirect(url_for('web.book_detail',isbn=isbn))


@web.route('/satisfy/wish/<int:wid>')
def satisfy_wish(wid):
    wish = Wish.query.get_or_404(wid)
    gift = Gift.query.filter_by(uid= current_user.id,isbn=wish.isbn).first()
    if not gift:
        flash("你还没上传此本书，请点击加入到赠送清单，添加前确保可以自己赠送此书")
    else:
        send_mail(wish.user.email,"有人想送你一本书",
                  "email/satisify_wish.html",wish=wish,gift=gift)
        flash("已向ta发送一封邮件，如果接受，你将收到鱼漂")
    return redirect(url_for("web.book_detail",isbn = wish.isbn))


@web.route('/wish/book/<isbn>/redraw')
def redraw_from_wish(isbn):
    wish = Wish.query.filter_by(isbn = isbn,launched = False).first_or_404()
    with db.auto_commit():
        wish.delete()
    return redirect(url_for("web.my_wish"))